package com.asj.vivo.lock.service.command.specific;

import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockPassword;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockPhone;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_lockRemarks;
import static com.asj.enums.DeviceIdentifierDictEnums.lockMachine;
import static com.asj.enums.DeviceIdentifierDictEnums.lockOperation;
import static com.asj.enums.DeviceIdentifierDictEnums.unlockMachine;
import static com.asj.enums.HttpRequestDictEnums.receiveLockResultUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.util.Log;

import com.asj.service.oppoSdk.OPPOControlService;
import com.asj.utils.HttpRequestUtils;
import com.asj.utils.JSONParamPackingUtils;
import com.google.gson.JsonObject;

/**
 * 设备锁机/解锁
 * */
public class SpecificLockOperationService {

    public static void lockOperation(JsonObject contentData, Context context){
        OPPOControlService oppoControlService = new OPPOControlService(context);
        //进行锁机
        String lockOp = contentData.get(lockOperation.getValue()).getAsString();
        if(lockMachine.getValue().equals(lockOp)){
            try{
                String lockPhone = contentData.get(json_key_lockPhone.getValue()).getAsString();
                String lockRemarks = contentData.get(json_key_lockRemarks.getValue()).getAsString();
                String lockPassword = contentData.get(json_key_lockPassword.getValue()).getAsString();

                oppoControlService.lockMachine(lockPassword,lockPhone,lockRemarks);

                JSONParamPackingUtils.functionPacking(contentData,result_success,null);
            }catch (Exception e){
                JSONParamPackingUtils.functionPacking(contentData,result_error,e.getMessage());
            }
        }
        //解锁
        else if(unlockMachine.getValue().equals(lockOp)){
            try{
                oppoControlService.unlockMachine(true);
                JSONParamPackingUtils.functionPacking(contentData,result_success,null);
            }catch (Exception e){
                JSONParamPackingUtils.functionPacking(contentData,result_error,e.getMessage());
            }
        }
        new Thread(()->{
            //将请求发送回去
            String result = HttpRequestUtils.retryHttpRequest(receiveLockResultUrl.getValue(), contentData.toString(), result_success.getCode(), 5,context);
            if(!result.equals(request_error.getMessage())){
                Log.i(lockOperation.getValue()+"结果已返回结果，告知成功！",result);
            }
        }).start();
    }
}
